package com.ly.heart.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.heart.mapper.KingBaseExchangeRateTypeMapper;
import com.ly.heart.service.IKingBaseExchangeRateTypeService;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.KingBaseExchangeRateTypeEntity;

import javax.annotation.Resource;

/**
 * 金蝶-汇率类型Service业务层处理
 *
 * @author cxl
 * @date 2024-06-23
 */
@Service
public class KingBaseExchangeRateTypeServiceImpl extends ServiceImpl<KingBaseExchangeRateTypeMapper, KingBaseExchangeRateTypeEntity> implements IKingBaseExchangeRateTypeService {

    @Resource
    private KingBaseExchangeRateTypeMapper kingBaseExchangeRateTypeMapper;

    /**
     * 查询金蝶-汇率类型
     *
     * @param id 金蝶-汇率类型ID
     * @return 金蝶-汇率类型
     */
    @Override
    public KingBaseExchangeRateTypeEntity selectKingBaseExchangeRateTypeEntityById ( String id ) {
        return kingBaseExchangeRateTypeMapper.selectKingBaseExchangeRateTypeEntityById ( id );
    }

    /**
     * 查询金蝶-汇率类型列表
     *
     * @param kingBaseExchangeRateTypeEntity 金蝶-汇率类型
     * @return 金蝶-汇率类型
     */
    @Override
    public Page selectKingBaseExchangeRateTypeEntityList ( KingBaseExchangeRateTypeEntity kingBaseExchangeRateTypeEntity ) {
        Page page = this.page ( new Page<> ( kingBaseExchangeRateTypeEntity.getPage (), kingBaseExchangeRateTypeEntity.getLimit () ) );
        return page;
    }

    /**
     * 新增金蝶-汇率类型
     *
     * @param kingBaseExchangeRateTypeEntity 金蝶-汇率类型
     * @return 结果
     */
    @Override
    public int insertKingBaseExchangeRateTypeEntity ( KingBaseExchangeRateTypeEntity kingBaseExchangeRateTypeEntity ) {
        kingBaseExchangeRateTypeEntity.setCreateTime ( DateUtils.getNowDate () );
        return kingBaseExchangeRateTypeMapper.insertKingBaseExchangeRateTypeEntity ( kingBaseExchangeRateTypeEntity );
    }

    /**
     * 修改金蝶-汇率类型
     *
     * @param kingBaseExchangeRateTypeEntity 金蝶-汇率类型
     * @return 结果
     */
    @Override
    public int updateKingBaseExchangeRateTypeEntity ( KingBaseExchangeRateTypeEntity kingBaseExchangeRateTypeEntity ) {
        return kingBaseExchangeRateTypeMapper.updateKingBaseExchangeRateTypeEntity ( kingBaseExchangeRateTypeEntity );
    }

    /**
     * 批量删除金蝶-汇率类型
     *
     * @param ids 需要删除的金蝶-汇率类型ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteKingBaseExchangeRateTypeEntityByIds ( String[] ids ) {
        List<KingBaseExchangeRateTypeEntity> list = this.list ( new LambdaQueryWrapper<KingBaseExchangeRateTypeEntity> ().in ( KingBaseExchangeRateTypeEntity :: getId, ids ) );
        List<String> idList = list.stream ().filter ( rateType -> rateType.getIsDel () == 1 ).map ( KingBaseExchangeRateTypeEntity :: getId ).collect ( Collectors.toList () );
        StringBuilder stringBuilder = new StringBuilder ( "【" );
        list.forEach ( rateType -> {
            if ( rateType.getIsDel () == 2 ) {
                stringBuilder.append ( rateType.getName () );
            }
        } );
        stringBuilder.append ( "】" );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseExchangeRateTypeEntity> ().set ( KingBaseExchangeRateTypeEntity :: getChangeTime, DateUtils.getNowDate () ).set ( KingBaseExchangeRateTypeEntity :: getChangeId, SecurityUtils.getLoginUser ().getUser ().getUserId () ).in ( KingBaseExchangeRateTypeEntity :: getId, idList ) );
        if ( ! update ) {
            throw new CustomException ( "删除汇率类型失败！" );
        }
        String str = "删除汇率类型成功";
        if ( list.size () > 0 ) {
            str.concat ( "，其中" ).concat ( stringBuilder.toString () ).concat ( "被引用，不可删除" );
        }
        return AjaxResult.success ( str );
    }

    /**
     * 删除金蝶-汇率类型信息
     *
     * @param id 金蝶-汇率类型ID
     * @return 结果
     */
    @Override
    public int deleteKingBaseExchangeRateTypeEntityById ( String id ) {
        return kingBaseExchangeRateTypeMapper.deleteKingBaseExchangeRateTypeEntityById ( id );
    }
}
